package com.cn.ccp.action.classschedule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.ClassScheduleBean;
import com.cn.ccp.bean.ClassScheduleDetailBean;
import com.cn.ccp.util.Contants;
import com.cn.ccp.util.MsgConstants;

/**
 * 课程表action
 * @author renyongchengO
 *O
 */
public class ClassScheduleDetailAction extends BaseAction{
	private Logger log = Logger.getLogger(ClassScheduleDetailAction.class);
	private ClassScheduleDetailBean bean;
	private String strTable;//课程表显示字符串
	private String strScheduleName;//课程表名称	
	private boolean showTeacherSchedule;//显示老师课程表
	
    public ClassScheduleDetailAction(){
    	initSchedule();
    }
    	 
    public String back(){
    	Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		Object showTeach  = varMap.get("showTeacherSchedule");
		if(showTeach != null && "true".equalsIgnoreCase(showTeach.toString()))
			return "toteacherlist";
		else
			return MAIN;
    }
    
    
	private void initSchedule(){
		
		
		String id = null;
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		id = varMap.get("id");
		if(id == null){
			Object objID = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Contants.TERM_SCHEDULE_ID);
			if(objID != null)
				id = objID.toString();
		}else
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Contants.TERM_SCHEDULE_ID,id);
		log.info("=============scheduleId = "+id);
		if(id == null || "".equalsIgnoreCase(id.trim()))
			return;
		Object showTeach = varMap.get("teacher");
		if(showTeach != null && "true".equalsIgnoreCase(showTeach.toString())){
			showTeacherSchedule = true;
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Contants.SHOW_TEACHER_SCHEDULE,showTeacherSchedule);
		}
		if(!showTeacherSchedule)	{
			Object objID = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Contants.SHOW_TEACHER_SCHEDULE);
			if(objID != null){
				showTeacherSchedule = "true".equalsIgnoreCase(objID.toString());
			}
		}
		log.info("=============showTeacherSchedule = "+showTeacherSchedule);
		ClassScheduleBean classScheduleBean = (ClassScheduleBean)getClassScheduleService().findClassScheduleById(Integer.parseInt(id));
		strScheduleName = classScheduleBean.getName();
		List<ClassScheduleDetailBean> allList = getClassScheduleDetailService().pageQuery(id);//返回系统课程表
    	int count = allList.size();
    	log.info("=============:count = "+count);
    	ClassScheduleDetailBean bean;
    	//拼写课程表字符串
    	StringBuffer buf = new StringBuffer();
    	buf.append("<table class='table table-bordered table-striped tab-b'>");
    	buf.append("<thead>");
    	buf.append("<tr>");
    	
		buf.append("<th class='col-odd' style='text-align:center;'>&nbsp;</th>");
		buf.append("<th class='col-odd' style='text-align:center;'>"+getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_MONDAY)+"</th>");
		buf.append("<th class='col-even' style='text-align:center;'>"+getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_TUESDAY)+"</th>");
		buf.append("<th class='col-odd' style='text-align:center;'>"+getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_WEDNESDAY)+"</th>");
		buf.append("<th class='col-even' style='text-align:center;'>"+getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_THURSDAY)+"</th>");
		buf.append("<th class='col-odd' style='text-align:center;'>"+getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_FRIDAY)+"</th>");
		buf.append("<th class='col-even' style='text-align:center;'>"+getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_SATURDAY)+"</th>");
		buf.append("<th class='col-odd' style='text-align:center;'>"+getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_SUNDAY)+"</th>");
    	buf.append("</tr>");
    	buf.append("</thead>");
    	
    	ArrayList<String> moningList = new ArrayList<String>();//早上课程表list
    	ArrayList<String> noonList = new ArrayList<String>();//上午课程表list
    	ArrayList<String> afterNoonList = new ArrayList<String>();//下午课程表list
    	ArrayList<String> nightList = new ArrayList<String>();//晚上课程表list
    	
    	StringBuffer temp = new StringBuffer();
    	for(int i=0; i<count; i++){
    		bean = allList.get(i);
    		log.info(bean.getScheduleSettingId()+":"+bean.getWeek() );
    		if(bean.getWeek() == 1){
				temp.append("<td class='col2'>");
			}else{
				temp.append("<td>");
			}
    		temp.append("<a href = 'pages/classschedule/editScheduleDetail.faces?id="+bean.getId()+"&showTeacherSchedule="+showTeacherSchedule+"'>");
			temp.append(bean.getSubjects());
			temp.append("</a>");
			temp.append("<br/>");
			if(!Contants.SCHEDULE_CLASS_SUBJECT_INIT.equalsIgnoreCase(bean.getSubjects())){
				temp.append("<i class='n14-icon-map-marker'></i>");
				temp.append(bean.getClassAddress().length()>10 ?bean.getClassAddress().substring(0,10)+"...":bean.getClassAddress() );
				temp.append("<br/>");
				temp.append(bean.getStartTime());
				temp.append("-");
				temp.append(bean.getEndTime());
				temp.append("<br/>");
				temp.append("<i class='n14-icon-calendar'></i>");
				temp.append(bean.getStartWeek());
				temp.append("-");
				temp.append(bean.getEndWeek());
				temp.append(getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_UNIT));
			}
			temp.append("</td>");
    		switch(bean.getScheduleType()){
				case Contants.SCHEDULE_TYPE_MORNING:
					if(bean.getWeek() == 7){
						moningList.add(temp.toString());
						temp = new StringBuffer();
					}
					break;
				case Contants.SCHEDULE_TYPE_FORENOON:
					if(bean.getWeek() == 7){
						noonList.add(temp.toString());
						temp = new StringBuffer();
					}
					break;
				case Contants.SCHEDULE_TYPE_AFTERNOON:
					if(bean.getWeek() == 7){
						afterNoonList.add(temp.toString());
						temp = new StringBuffer();
					}
					break;
				case Contants.SCHEDULE_TYPE_NIGHT:
					if(bean.getWeek() == 7){
						nightList.add(temp.toString());
						temp = new StringBuffer();
					}
					break;
			}
    	}
    	//设置早上课程表
    	count = moningList.size();
    	for(int i=0; i<count; i++){
    		buf.append("<tr>");
    		if(i==0){
    			if(count >1)
	    			buf.append("<td rowspan='"+count+"' align='center'>"+getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_MORNING)+"</td>");
	    		else if(count == 1)
	    			buf.append("<td align='center'>"+getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_MORNING)+"</td>");
    		}
    		buf.append(moningList.get(i));
    		buf.append("</tr>");
    	}
    	
    	//设置上午课程表
    	count = noonList.size();
    	for(int i=0; i<count; i++){
    		buf.append("<tr>");
    		if(i==0){
    			if(count >1)
	    			buf.append("<td rowspan='"+count+"' align='center'>"+getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_FORENOON)+"</td>");
	    		else if(count == 1)
	    			buf.append("<td align='center'>"+getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_FORENOON)+"</td>");
    		}
    		buf.append(noonList.get(i));
    		buf.append("</tr>");
    	}
    	
    	//设置下午课程表
    	count = afterNoonList.size();
    	for(int i=0; i<count; i++){
    		buf.append("<tr>");
    		if(i==0){
    			if(count >1)
	    			buf.append("<td rowspan='"+count+"' align='center'>"+getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_AFTERNOON)+"</td>");
	    		else if(count == 1)
	    			buf.append("<td align='center'>"+getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_AFTERNOON)+"</td>");
    		}
    		buf.append(afterNoonList.get(i));
    		buf.append("</tr>");
    	}
    	
    	//设置晚上课程表
    	count = nightList.size();
    	for(int i=0; i<count; i++){
    		buf.append("<tr>");
    		if(i==0){
    			if(count >1)
	    			buf.append("<td rowspan='"+count+"' align='center'>"+getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_NIGHT)+"</td>");
	    		else if(count == 1)
	    			buf.append("<td align='center'>"+getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_NIGHT)+"</td>");
    		}
    		buf.append(nightList.get(i));
    		buf.append("</tr>");
    	}
    	
    	buf.append("</table>");
    	strTable = buf.toString();
//    	log.info(strTable);
	}

	public String goEdit(){
		log.info("----------------goEdit()----------------------");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		log.info("-----------id = "+id); 
		if(id != null)
			bean = (ClassScheduleDetailBean)getClassScheduleDetailService().findClassScheduleById(Integer.parseInt(id));
		return UPDATE;
	}
	 

	public ClassScheduleDetailBean getBean() {
		if(bean == null)
			bean  = new ClassScheduleDetailBean();
		return bean;
	}

	public void setBean(ClassScheduleDetailBean bean) {
		this.bean = bean;
	}

	public String getStrTable() {
		return strTable;
	}

	public void setStrTable(String strTable) {
		this.strTable = strTable;
	}

	public String getStrScheduleName() {
		return strScheduleName;
	}

	public void setStrScheduleName(String strScheduleName) {
		this.strScheduleName = strScheduleName;
	}

	public boolean isShowTeacherSchedule() {
		return showTeacherSchedule;
	}

	public void setShowTeacherSchedule(boolean showTeacherSchedule) {
		this.showTeacherSchedule = showTeacherSchedule;
	}
		
}
